<?php

/*

  Action - Attack	$5

  +2 Cards
  Each other player with 4 or more cards in hand puts cards from his hand on top of his deck until he has 3 cards in his hand.

 */

class GhostShip extends Card implements iAction, iKingdom, iAttack, iSeaside {

    public function __construct() {
        parent::__construct("Ghost Ship", 5);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+2 Cards",
            "Each other player with 4 or more cards in hand puts cards from his hand on top of his deck until he has 3 cards in his hand.");
    }

    public function play(Game $game, $isRealCard) {
        $drawed_cards = $game->drawCards(2);
        ScreenDisplay::addScreenMessage($game, true, "draw " . nicePrint($drawed_cards), " draws " . sizeof($drawed_cards) . " cards");

        $game->setPhase(Phase_Sleep);
        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $playHandOther = $game->getPlayHand($name);
            $playHandSize = sizeof($playHandOther);
            if ($playHandSize > 3) {
                $cards_to_return = $playHandSize - 3;
                $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHandOther, $cards_to_return, $cards_to_return, null, "Chose $cards_to_return cards to return to draw deck", $name, true);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            $return_cards_obj = array();
            foreach ($output as $p) {
                $return_cards_obj[] = n2c($p);
            }
            $output = MySocket::GetUserReply($game, Input_Reorder, $return_cards_obj, -1, -1, null, "Choose the order", $name, true);
        }

        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {
            ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " cards on top of your deck", "puts " . sizeof($output) . " cards on top of his deck", $name);
            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->moveCardFromPileToPile($p, PlayHand, DrawPile, $name);
            }
            $game->setPhase(Phase_Sleep, $name);
        }
        $game->cardActionFinished();
        return true;
    }

}

?>